package com.example.shiwu;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.shiwu.db.model.CvsBean;
import com.example.shiwu.db.model.Sends;
import com.example.shiwu.manager.DynamicLineChartManager;
import com.example.shiwu.service.SendsService;
import com.example.shiwu.util.ContextVariable;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class SendReceiveImgDetailsActivity extends AppCompatActivity {
    private DynamicLineChartManager dynamicLineChartManager;
    private List<Integer> list = new ArrayList<>(); //数据集合
    private List<String> names = new ArrayList<>(); //折线名字集合
    private List<Integer> colour = new ArrayList<>();//折线颜色集合
    public TextView title_text, title_setting;
    public ImageView title_back;

    private static final String TAG = MainActivity.class.getSimpleName();

    private ListView mRecyclerView;

    private List<CvsBean> mList;
    private SendsService sendService;
    private ImageView mChart1, mChart2, mChart3, mChart4;
    private TextView device_type_val,send_frequency_val,receive_voltage_val;
    public TextView plan_break, permutations, waveform, frequency_spectrum;
    public int gatherType = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_iteminfo_img_edit);
        title_back = findViewById(R.id.gather_title_back);
        //TitleSettingUtil.initTitle("详情",null,title_back,title_text,title_setting);
        title_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SendReceiveImgDetailsActivity.this.finish();
            }
        });
        sendService = new SendsService();
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        Sends sends = new Sends();
        sends.setId(Integer.parseInt(id));
        List<Sends> sendslis = sendService.getSends(sends);
        String fname = "";
        mChart1 = findViewById(R.id.imageViews);
        mChart2 = findViewById(R.id.imageViews2);
        mChart3 = findViewById(R.id.imageViews3);
        mChart4 = findViewById(R.id.imageViews4);
        device_type_val = findViewById(R.id.device_type_val);
        send_frequency_val = findViewById(R.id.send_frequency_val);
        receive_voltage_val = findViewById(R.id.receive_voltage_val);
        plan_break = findViewById(R.id.plan_break);
        permutations = findViewById(R.id.permutations);
        waveform = findViewById(R.id.waveform);
        frequency_spectrum = findViewById(R.id.frequency_spectrum);
        Resources res = getResources();
        String[] freqData = res.getStringArray(R.array.freqData);;
        String[] modeData = res.getStringArray(R.array.modeData);
        if(sendslis.size()>0){
            String uid = sendslis.get(0).getUid();
            String itemid = String.valueOf(sendslis.get(0).getItemId());
            fname = ContextVariable.IMG_DOWN_NAME+itemid+"-"+uid;
            String freq_Indx = freqData[Integer.parseInt(sendslis.get(0).getSendFrequency())];
            device_type_val.setText(modeData[sendslis.get(0).getType()]);
            send_frequency_val.setText(freq_Indx + "hz");
            receive_voltage_val.setText(sendslis.get(0).getVoltage() + "v");
        }

//        plan_break.setTextColor(Color.parseColor("#FF007AFF"));
//        plan_break.setTextSize(18);
//        File file=new File(fname+"_niduan.png");
//        mChart1.setImageURI(Uri.fromFile(file));
        plan_break.setVisibility(View.INVISIBLE);
        permutations.setVisibility(View.INVISIBLE);
        waveform.setText("");
        frequency_spectrum.setVisibility(View.INVISIBLE);

        setImgSrc(fname);
    }

    private void setImgSrc(String fileName) {
        File file=new File(fileName+"_niduan.png");
        mChart1.setImageURI(Uri.fromFile(file));
        File file2=new File(fileName+"_pailie.png");
        mChart2.setImageURI(Uri.fromFile(file2));
        File file3=new File(fileName+"_boxing.png");
        mChart3.setImageURI(Uri.fromFile(file3));
        File file4=new File(fileName+"_pinpu.png");
        mChart4.setImageURI(Uri.fromFile(file4));
    }
    public void onClick(View v) {
        displayChart();
        switch (v.getId()) {
            case R.id.plan_break:
                planBreak();
                break;
            case R.id.permutations:
                permutations();
                break;
            case R.id.waveform:
                waveform();
                break;
            case R.id.frequency_spectrum:
                frequencySpectrum();
                break;
            default:
                break;
        }

    }
    public void displayChart() {
        mChart1.setVisibility(View.GONE);
        mChart2.setVisibility(View.GONE);
        mChart3.setVisibility(View.GONE);
        mChart4.setVisibility(View.GONE);
    }
    private void planBreak() {
        mChart1.setVisibility(View.VISIBLE);
        mChart2.setVisibility(View.GONE);
        mChart3.setVisibility(View.GONE);
        mChart4.setVisibility(View.GONE);
        plan_break.setTextColor(Color.parseColor("#FF007AFF"));
        permutations.setTextColor(Color.parseColor("#A6000000"));
        waveform.setTextColor(Color.parseColor("#A6000000"));
        frequency_spectrum.setTextColor(Color.parseColor("#A6000000"));
        plan_break.setTextSize(18);
        permutations.setTextSize(16);
        waveform.setTextSize(16);
        waveform.setTextSize(16);
    }
    private void permutations() {
        mChart2.setVisibility(View.VISIBLE);
        mChart1.setVisibility(View.GONE);
        mChart3.setVisibility(View.GONE);
        mChart4.setVisibility(View.GONE);
        plan_break.setTextColor(Color.parseColor("#A6000000"));
        permutations.setTextColor(Color.parseColor("#FF007AFF"));
        waveform.setTextColor(Color.parseColor("#A6000000"));
        frequency_spectrum.setTextColor(Color.parseColor("#A6000000"));

        plan_break.setTextSize(16);
        permutations.setTextSize(18);
        waveform.setTextSize(16);
        waveform.setTextSize(16);

    }

    private void waveform() {
        mChart3.setVisibility(View.VISIBLE);
        mChart1.setVisibility(View.GONE);
        mChart2.setVisibility(View.GONE);

        mChart4.setVisibility(View.GONE);
        plan_break.setTextColor(Color.parseColor("#A6000000"));
        permutations.setTextColor(Color.parseColor("#A6000000"));
        waveform.setTextColor(Color.parseColor("#FF007AFF"));
        frequency_spectrum.setTextColor(Color.parseColor("#A6000000"));
        plan_break.setTextSize(16);
        permutations.setTextSize(16);
        waveform.setTextSize(18);
        frequency_spectrum.setTextSize(16);

    }

    private void frequencySpectrum() {
        mChart4.setVisibility(View.VISIBLE);
        mChart1.setVisibility(View.GONE);
        mChart2.setVisibility(View.GONE);
        mChart3.setVisibility(View.GONE);

        plan_break.setTextColor(Color.parseColor("#A6000000"));
        permutations.setTextColor(Color.parseColor("#A6000000"));
        waveform.setTextColor(Color.parseColor("#A6000000"));
        frequency_spectrum.setTextColor(Color.parseColor("#FF007AFF"));
        plan_break.setTextSize(16);
        permutations.setTextSize(16);
        waveform.setTextSize(16);
        frequency_spectrum.setTextSize(18);

    }
}